IF(BUILD_PYTHON_INTERFACE)
	INCLUDE(FindPythonSitePackages.cmake)
	
	# Find out the dependencies for the python core module interface
	FILE(GLOB CORE_HEADER_FILES ../Core/*.h)

	# Find where SWIG and Python libraries are installed
	FIND_PACKAGE(SWIG REQUIRED)
	FIND_PACKAGE(PythonLibs REQUIRED)

	# Set where to find the python header files
	INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})

	# Add the swig command to the build
	ADD_CUSTOM_COMMAND(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Core_wrap.cxx"
#	COMMAND "${SWIG_EXECUTABLE}" -c++ -python -outdir "${REAL_OUTPUT_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/Core.i"
	COMMAND "${SWIG_EXECUTABLE}" -c++ -python -outdir "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/Core.i"
	MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/Core.i"
	DEPENDS ${CORE_HEADER_FILES}
	COMMENT "Generating SWIG wrapper code...")

	if(COMMAND cmake_policy)
			cmake_policy(SET CMP0003 NEW)
		endif(COMMAND cmake_policy)

	# Create the python core interface library
	ADD_LIBRARY(_Core MODULE Core.i Core_wrap.cxx)

	SET_SOURCE_FILES_PROPERTIES(Core_wrap.cxx PROPERTIES GENERATED 1)

	# Make sure the output library has the correct name (e.g. _Core_d.pyd for windows debug, _Core.so for linux release)
	SET_TARGET_PROPERTIES(_Core PROPERTIES PREFIX "" DEBUG_POSTFIX "_d")
	IF(WIN32)
		SET_TARGET_PROPERTIES(_Core PROPERTIES SUFFIX ".pyd")	
	ENDIF(WIN32)

	IF(UNIX)
		SET_TARGET_PROPERTIES(_Core PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1
			BUILD_WITH_INSTALL_RPATH 1
			INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
#			LINK_FLAGS "-Xlinker")
	ENDIF(UNIX)

	# Link to TexGenCore and Python
	TARGET_LINK_LIBRARIES(_Core
	TexGenCore
	optimized ${PYTHON_LIBRARIES})

	# If building the debug version link to the python debug library
	IF(PYTHON_DEBUG_LIBRARIES)
		TARGET_LINK_LIBRARIES(_Core debug ${PYTHON_DEBUG_LIBRARIES})
	ENDIF(PYTHON_DEBUG_LIBRARIES)
	
	SET(PYTHON_MODULES "'Abaqus', 'Ansys', 'Completer', 'FlowTex', 'GridFile', 'TexGenv2', 'WiseTex'")
	IF(BUILD_RENDERER)
		SET(PYTHON_MODULES "${PYTHON_MODULES}, 'Renderer'")
	ENDIF(BUILD_RENDERER)
	IF(BUILD_CASCADE_EXPORT)
		SET(PYTHON_MODULES "${PYTHON_MODULES}, 'Export'")
	ENDIF(BUILD_CASCADE_EXPORT)

	FILE(WRITE "__init__.py" "# __all__ = [${PYTHON_MODULES}]")

	INSTALL(FILES __init__.py Core.py
	DESTINATION ${PYTHON_SITEPACKAGES_DIR}/TexGen)


	# Find the TexGen python modules
	FILE(GLOB PYTHON_MODULE_FILES Lib/*.py)

	INSTALL(FILES ${PYTHON_MODULE_FILES}
	DESTINATION ${PYTHON_SITEPACKAGES_DIR}/TexGen)

	# Find the sample python scripts
	FILE(GLOB PYTHON_SAMPLE_SCRIPTS Scripts/*.py)

	INSTALL(TARGETS _Core
		LIBRARY DESTINATION ${PYTHON_SITEPACKAGES_DIR}/TexGen)

	IF(WIN32)
		INSTALL(FILES ${PYTHON_SAMPLE_SCRIPTS}
		DESTINATION ${CMAKE_INSTALL_PREFIX}/Scripts)
		INSTALL(TARGETS _Core
		LIBRARY DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../Installer)	
	ELSE(WIN32)
		INSTALL(FILES ${PYTHON_SAMPLE_SCRIPTS}
		DESTINATION ${CMAKE_INSTALL_PREFIX}/share/TexGen/Scripts)		
	ENDIF(WIN32)
	
	# Build the renderer python module
	IF(BUILD_RENDERER)
		# Find out the dependencies for the python renderer module interface
		FILE(GLOB RENDERER_HEADER_FILES ../Renderer/*.h)

		FIND_PACKAGE(VTK 6.3 REQUIRED NO_MODULE)

		INCLUDE_DIRECTORIES(${VTK_INCLUDE_DIRS})

		ADD_CUSTOM_COMMAND(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Renderer_wrap.cxx"
#		COMMAND "${SWIG_EXECUTABLE}" -c++ -python -outdir "${REAL_OUTPUT_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/Renderer.i"
		COMMAND "${SWIG_EXECUTABLE}" -c++ -python -outdir "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/Renderer.i"
		MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/Renderer.i"
		DEPENDS ${RENDERER_HEADER_FILES}
		COMMENT "Generating SWIG wrapper code...")

		ADD_LIBRARY(_Renderer MODULE Renderer.i Renderer_wrap.cxx)

		SET_SOURCE_FILES_PROPERTIES(Renderer_wrap.cxx PROPERTIES GENERATED 1)

		SET_TARGET_PROPERTIES(_Renderer PROPERTIES PREFIX "" DEBUG_POSTFIX "_d")
		IF(WIN32)
			SET_TARGET_PROPERTIES(_Renderer PROPERTIES SUFFIX ".pyd")	
		ENDIF(WIN32)
		IF(UNIX)
			SET_TARGET_PROPERTIES(_Renderer PROPERTIES COMPILE_FLAGS -Wno-deprecated)
		ENDIF(UNIX)

		IF(UNIX)
			SET_TARGET_PROPERTIES(_Renderer PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1
				BUILD_WITH_INSTALL_RPATH 1
				INSTALL_RPATH "${VTK_LIBRARY_DIRS};${CMAKE_INSTALL_PREFIX}/lib")
	#			LINK_FLAGS "-Xlinker")
			SET_TARGET_PROPERTIES(_Renderer PROPERTIES LINK_FLAGS "-Wl,-rpath,${VTK_DIR} -L${VTK_DIR}")
		ENDIF(UNIX)

		TARGET_LINK_LIBRARIES(_Renderer
		TexGenRenderer
		${VTK_LIBRARIES}
		optimized ${PYTHON_LIBRARIES})
		IF(PYTHON_DEBUG_LIBRARIES)
			TARGET_LINK_LIBRARIES(_Renderer debug ${PYTHON_DEBUG_LIBRARIES})
		ENDIF(PYTHON_DEBUG_LIBRARIES)

		INSTALL(TARGETS _Renderer
		LIBRARY DESTINATION ${PYTHON_SITEPACKAGES_DIR}/TexGen)

		INSTALL(FILES Renderer.py
		DESTINATION ${PYTHON_SITEPACKAGES_DIR}/TexGen)

		IF(WIN32)
			INSTALL(TARGETS _Renderer
			LIBRARY DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../Installer)	
		ENDIF(WIN32)

	ENDIF(BUILD_RENDERER)


	# Build the renderer python module
	IF(BUILD_CASCADE_EXPORT)
		SET(EXPORT_HEADER_FILES ../Export/Exporter.h)

		ADD_CUSTOM_COMMAND(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Export_wrap.cxx"
#		COMMAND "${SWIG_EXECUTABLE}" -c++ -python -outdir "${REAL_OUTPUT_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/Export.i"
		COMMAND "${SWIG_EXECUTABLE}" -c++ -python -outdir "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/Export.i"
		MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/Export.i"
		DEPENDS ${EXPORT_HEADER_FILES}
		COMMENT "Generating SWIG wrapper code...")

		ADD_LIBRARY(_Export MODULE Export.i Export_wrap.cxx)
		
		SET_SOURCE_FILES_PROPERTIES(Export_wrap.cxx PROPERTIES GENERATED 1)

		SET_TARGET_PROPERTIES(_Export PROPERTIES PREFIX "" DEBUG_POSTFIX "_d")
		IF(WIN32)
			SET_TARGET_PROPERTIES(_Export PROPERTIES SUFFIX ".pyd")	
		ENDIF(WIN32)
		IF(UNIX)
			SET_TARGET_PROPERTIES(_Export PROPERTIES COMPILE_FLAGS -Wno-deprecated)
		ENDIF(UNIX)
		IF(UNIX)
			SET_TARGET_PROPERTIES(_Export PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1
				BUILD_WITH_INSTALL_RPATH 1
				INSTALL_RPATH "${OPENCASCADE_LIBRARY_DIR};${CMAKE_INSTALL_PREFIX}/lib")
	#			LINK_FLAGS "-Xlinker")
		ENDIF(UNIX)
		TARGET_LINK_LIBRARIES(_Export
		TexGenExport
		optimized ${PYTHON_LIBRARIES})
		IF(PYTHON_DEBUG_LIBRARIES)
			TARGET_LINK_LIBRARIES(_Export debug ${PYTHON_DEBUG_LIBRARIES})
		ENDIF(PYTHON_DEBUG_LIBRARIES)

		INSTALL(TARGETS _Export
		LIBRARY DESTINATION ${PYTHON_SITEPACKAGES_DIR}/TexGen)

		INSTALL(FILES Export.py
		DESTINATION ${PYTHON_SITEPACKAGES_DIR}/TexGen)

		IF(WIN32)
			INSTALL(TARGETS _Export
			LIBRARY DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/../Installer)	
		ENDIF(WIN32)

	ENDIF(BUILD_CASCADE_EXPORT)

ENDIF(BUILD_PYTHON_INTERFACE)




